using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

using CarlosAg.ExcelXmlWriter;

namespace Webcam_Test
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		
		private WebCam_Capture.WebCamCapture WebCamCapture;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Button cmdStart;
		private System.Windows.Forms.Button cmdStop;
		private System.Windows.Forms.Button cmdContinue;
		private System.Windows.Forms.NumericUpDown numCaptureTime;
		private System.Windows.Forms.Label label1;


        ///////////////

        double[,] val = new double[22, 3];
        double[] dif = new double[22];
        //datos tabla

        
        public static int[] gris = new int[256];
        public static int[] umbral = new int[256];
        public static int[] objetos = new int[1000];
        public Bitmap bmp, bmp_gris, bmp_etiqueta, webBmp;
        int[,] matriz, matrizBin, matB;
        int num = 2;
        int area_minima = 10;
        string[,] tabla = new string[100, 10];
        int objeto = 1;
        double el = 0;
        public int index = 0;
        double teta;
        DirectoryInfo dir = new DirectoryInfo(@"C:\Fotos\Peine\");
        double N02 = 0;
        double N03 = 0;
        double N10 = 0;
        double N20 = 0;
        double N30 = 0;
        double N11 = 0;
        double N12 = 0;
        double N21 = 0;
        double ar = 0;
        double M1 = 0, M2 = 0, M3 = 0, M4 = 0, M5 = 0, M6 = 0, M7 = 0;
        int x_centro_img;
        int y_centro_img;
        double x, y;
        private GroupBox groupBox2;
        private Button b_calcular;
        private Button b_excel;
        private NumericUpDown numericUpDown1;
        private Button button3;
        private Button b_identifica;
        private TextBox textBox1;
        private TextBox tb_abrir;
        private Button b_abrir;
        private PictureBox pb_etiquetas;
        private PictureBox pbUmbral;
        private GroupBox groupBox3;
        private GroupBox groupBox4;
        private GroupBox groupBox5;
        private GroupBox groupBox6;
        private GroupBox groupBox7;
        private Label label2;
        private TextBox tb_objetos;
        private Label label5;
        private NumericUpDown n_area;
        private TextBox tb_area;
        private Label l_centroide;
        private Label l_x;
        private Label l_y;
        private Button button4;
        private TextBox tb_y_img;
        private TextBox tb_x_img;
        private GroupBox groupBox1;
        private GroupBox groupBox8;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
            tabla[0, 0] = "Objetos";
            tabla[0, 1] = "M1";
            tabla[0, 2] = "M2";
            tabla[0, 3] = "M3";
            tabla[0, 4] = "M4";
            tabla[0, 5] = "M5";
            tabla[0, 6] = "M6";
            tabla[0, 7] = "M7";
            tabla[0, 8] = "ELONGACION";
            tabla[0, 9] = "angulo";
            ////valores de tabla val

            //billete
            val[0,0] = 0.190464528;//M1
            val[0,1] = 0.011065266;//M2
            val[0, 2] = 3.128399345;//el

            //cartera
            val[1,0] = 0.249551052;//M1
            val[1,1] = 0.034051222;//M2
            val[1, 2] = 6.668208187;//el

            //clavo
            val[2,0]=1.788630214;
            val[2,1]=3.512441372;
            val[2, 2] = 439.5390289;
            
            //cuchara
            val[3, 0] = 0.955454024;//M1
            val[3, 1] = 0.861947484;//M2
            val[3, 2] = 66.4659476;//el

            //cuchillo
            val[4, 0] = 0.842152502;//M1
            val[4, 1] = 0.677895248;//M2
            val[4, 2] = 86.77576649;//el
            		

             //desarmador
            val[5, 0] = 0.657529262;//M1
            val[5, 1] = 0.404382816;//M2
            val[5, 2] = 36.42410689;//el
            		

             //cepillo
            val[6, 0] = 1.244296286;//M1
            val[6, 1] = 1.516161082;//M2
            val[6, 2] = 149.6583313;//el
		
            ////////////////////

             //lapiz
            val[7, 0] = 2.300241778;//M1
            val[7, 1] = 5.305933456;//M2
            val[7, 2] = 744.6087649;//el

             //libro
            val[8, 0] = 0.166684948;//M1
            val[8, 1] = 4.49242E-05;//M2
            val[8, 2] = 1.080849272;//el	

             //llave
            val[9, 0] = 0.278399472;//M1
            val[9, 1] = 0.021666043;//M2
            val[9, 2] = 3.242149881;//el
            		
             //martillo
            val[10, 0] = 1.118630711;//M1
            val[10, 1] = 1.150215021;//M2
            val[10, 2] = 47.31869105;//el
            		

             //moneda
            val[11, 0] = 0.159786067;//M1
            val[11, 1] = 0.000184443;//M2
            val[11, 2] = 1.175184767;//el
            		

             //peine
            val[12, 0] = 0.406530856;//M1
            val[12, 1] = 0.12728657;//M2
            val[12, 2] = 15.19529518;//el
           				

/////////////////////////////
             //pinzas abiertas
            val[13, 0] = 0.729648175;//M1
            val[13, 1] = 0.097108176;//M2
            val[13, 2] = 2.500877242;//el
            		

             //pinzas cerradas
            val[14, 0] = 0.613039262;//M1
            val[14, 1] = 0.255509366;//M2
            val[14, 2] = 10.4875604;//el
            		

             //plato
            val[15, 0] = 0.159215063;//M1
            val[15, 1] = 0.0000175315;//M2
            val[15, 2] = 1.051927514;//el
            		

             //pluma
            val[16, 0] = 1.095103687;//M1
            val[16, 1] = 1.179003877;//M2
            val[16, 2] = 165.540745;//el
            		

             //stealson
            val[17, 0] = 0.52709446;//M1
            val[17, 1] = 0.232808728;//M2
            val[17, 2] = 22.41197591;//el
            		

             //tenedor
            val[18, 0] = 1.424378764;//M1
            val[18, 1] = 1.971683185;//M2
            val[18, 2] = 105.2172929;//el
            		
/////////////////////

            //tijeras abiertas
            val[19, 0] = 0.904926568;//M1
            val[19, 1] = 0.608005911;//M2
            val[19, 2] = 13.33486776;//el
            		

            //tijeras cerradas
            val[20, 0] = 0.709713476;//M1
            val[20, 1] = 0.331600897;//M2
            val[20, 2] = 9.281022322;//el
            		
            //tornillo
            val[21, 0] = 0.223785635;//M1
            val[21, 1] = 0.013325138;//M2
            val[21, 2] = 2.924326578;//el
		

            /*
             //
            val[, 0] = ;//M1
            val[, 1] = ;//M2
            val[, 2] = ;//el
            */
            		

            	

            		

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.WebCamCapture = new WebCam_Capture.WebCamCapture();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.cmdStart = new System.Windows.Forms.Button();
            this.cmdStop = new System.Windows.Forms.Button();
            this.cmdContinue = new System.Windows.Forms.Button();
            this.numCaptureTime = new System.Windows.Forms.NumericUpDown();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.b_calcular = new System.Windows.Forms.Button();
            this.b_excel = new System.Windows.Forms.Button();
            this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
            this.button3 = new System.Windows.Forms.Button();
            this.b_identifica = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.tb_abrir = new System.Windows.Forms.TextBox();
            this.b_abrir = new System.Windows.Forms.Button();
            this.pb_etiquetas = new System.Windows.Forms.PictureBox();
            this.pbUmbral = new System.Windows.Forms.PictureBox();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.groupBox4 = new System.Windows.Forms.GroupBox();
            this.groupBox5 = new System.Windows.Forms.GroupBox();
            this.groupBox6 = new System.Windows.Forms.GroupBox();
            this.groupBox7 = new System.Windows.Forms.GroupBox();
            this.label2 = new System.Windows.Forms.Label();
            this.tb_objetos = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.n_area = new System.Windows.Forms.NumericUpDown();
            this.tb_area = new System.Windows.Forms.TextBox();
            this.l_centroide = new System.Windows.Forms.Label();
            this.l_x = new System.Windows.Forms.Label();
            this.l_y = new System.Windows.Forms.Label();
            this.button4 = new System.Windows.Forms.Button();
            this.tb_y_img = new System.Windows.Forms.TextBox();
            this.tb_x_img = new System.Windows.Forms.TextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox8 = new System.Windows.Forms.GroupBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.numCaptureTime)).BeginInit();
            this.groupBox2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pb_etiquetas)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbUmbral)).BeginInit();
            this.groupBox3.SuspendLayout();
            this.groupBox4.SuspendLayout();
            this.groupBox5.SuspendLayout();
            this.groupBox6.SuspendLayout();
            this.groupBox7.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.n_area)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.groupBox8.SuspendLayout();
            this.SuspendLayout();
            // 
            // WebCamCapture
            // 
            this.WebCamCapture.CaptureHeight = 240;
            this.WebCamCapture.CaptureWidth = 320;
            this.WebCamCapture.FrameNumber = ((ulong)(0ul));
            this.WebCamCapture.Location = new System.Drawing.Point(17, 17);
            this.WebCamCapture.Name = "WebCamCapture";
            this.WebCamCapture.Size = new System.Drawing.Size(342, 252);
            this.WebCamCapture.TabIndex = 0;
            this.WebCamCapture.TimeToCapture_milliseconds = 100;
            this.WebCamCapture.ImageCaptured += new WebCam_Capture.WebCamCapture.WebCamEventHandler(this.WebCamCapture_ImageCaptured);
            // 
            // pictureBox1
            // 
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Location = new System.Drawing.Point(15, 55);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(200, 160);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // cmdStart
            // 
            this.cmdStart.Location = new System.Drawing.Point(6, 19);
            this.cmdStart.Name = "cmdStart";
            this.cmdStart.Size = new System.Drawing.Size(78, 24);
            this.cmdStart.TabIndex = 1;
            this.cmdStart.Text = "Webcam";
            this.cmdStart.Click += new System.EventHandler(this.cmdStart_Click);
            // 
            // cmdStop
            // 
            this.cmdStop.Location = new System.Drawing.Point(90, 19);
            this.cmdStop.Name = "cmdStop";
            this.cmdStop.Size = new System.Drawing.Size(78, 24);
            this.cmdStop.TabIndex = 2;
            this.cmdStop.Text = "Tomar";
            this.cmdStop.Click += new System.EventHandler(this.cmdStop_Click);
            // 
            // cmdContinue
            // 
            this.cmdContinue.Location = new System.Drawing.Point(174, 19);
            this.cmdContinue.Name = "cmdContinue";
            this.cmdContinue.Size = new System.Drawing.Size(78, 24);
            this.cmdContinue.TabIndex = 3;
            this.cmdContinue.Text = "Reiniciar";
            this.cmdContinue.Click += new System.EventHandler(this.cmdContinue_Click);
            // 
            // numCaptureTime
            // 
            this.numCaptureTime.Location = new System.Drawing.Point(227, 47);
            this.numCaptureTime.Maximum = new decimal(new int[] {
            32000,
            0,
            0,
            0});
            this.numCaptureTime.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.numCaptureTime.Name = "numCaptureTime";
            this.numCaptureTime.Size = new System.Drawing.Size(66, 21);
            this.numCaptureTime.TabIndex = 4;
            this.numCaptureTime.Value = new decimal(new int[] {
            20,
            0,
            0,
            0});
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(18, 50);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(203, 18);
            this.label1.TabIndex = 5;
            this.label1.Text = "Tiempo de captura(Millisegundos)";
            // 
            // groupBox2
            // 
            this.groupBox2.BackColor = System.Drawing.SystemColors.Control;
            this.groupBox2.Controls.Add(this.b_identifica);
            this.groupBox2.Controls.Add(this.b_excel);
            this.groupBox2.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.groupBox2.ForeColor = System.Drawing.SystemColors.Desktop;
            this.groupBox2.Location = new System.Drawing.Point(509, 369);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.groupBox2.Size = new System.Drawing.Size(323, 148);
            this.groupBox2.TabIndex = 17;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "CONTROLES";
            // 
            // b_calcular
            // 
            this.b_calcular.Enabled = false;
            this.b_calcular.Location = new System.Drawing.Point(6, 29);
            this.b_calcular.Name = "b_calcular";
            this.b_calcular.Size = new System.Drawing.Size(105, 23);
            this.b_calcular.TabIndex = 24;
            this.b_calcular.Text = "Calcular";
            this.b_calcular.UseVisualStyleBackColor = true;
            this.b_calcular.Click += new System.EventHandler(this.b_calcular_Click);
            // 
            // b_excel
            // 
            this.b_excel.Enabled = false;
            this.b_excel.Location = new System.Drawing.Point(8, 23);
            this.b_excel.Name = "b_excel";
            this.b_excel.Size = new System.Drawing.Size(105, 23);
            this.b_excel.TabIndex = 24;
            this.b_excel.Text = "Excel";
            this.b_excel.UseVisualStyleBackColor = true;
            this.b_excel.Click += new System.EventHandler(this.b_excel_Click);
            // 
            // numericUpDown1
            // 
            this.numericUpDown1.Location = new System.Drawing.Point(130, 48);
            this.numericUpDown1.Maximum = new decimal(new int[] {
            255,
            0,
            0,
            0});
            this.numericUpDown1.Name = "numericUpDown1";
            this.numericUpDown1.Size = new System.Drawing.Size(50, 20);
            this.numericUpDown1.TabIndex = 7;
            this.numericUpDown1.Value = new decimal(new int[] {
            130,
            0,
            0,
            0});
            // 
            // button3
            // 
            this.button3.Enabled = false;
            this.button3.Location = new System.Drawing.Point(186, 48);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(105, 20);
            this.button3.TabIndex = 6;
            this.button3.Text = "Umbral binario";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // b_identifica
            // 
            this.b_identifica.Enabled = false;
            this.b_identifica.Location = new System.Drawing.Point(11, 52);
            this.b_identifica.Name = "b_identifica";
            this.b_identifica.Size = new System.Drawing.Size(102, 23);
            this.b_identifica.TabIndex = 30;
            this.b_identifica.Text = "Identifica";
            this.b_identifica.UseVisualStyleBackColor = true;
            this.b_identifica.Click += new System.EventHandler(this.b_identifica_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(134, 19);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.textBox1.Size = new System.Drawing.Size(304, 124);
            this.textBox1.TabIndex = 26;
            // 
            // tb_abrir
            // 
            this.tb_abrir.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tb_abrir.Location = new System.Drawing.Point(22, 19);
            this.tb_abrir.Name = "tb_abrir";
            this.tb_abrir.Size = new System.Drawing.Size(311, 21);
            this.tb_abrir.TabIndex = 32;
            // 
            // b_abrir
            // 
            this.b_abrir.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.b_abrir.Location = new System.Drawing.Point(339, 19);
            this.b_abrir.Name = "b_abrir";
            this.b_abrir.Size = new System.Drawing.Size(75, 23);
            this.b_abrir.TabIndex = 31;
            this.b_abrir.Text = "Abrir";
            this.b_abrir.UseVisualStyleBackColor = true;
            this.b_abrir.Click += new System.EventHandler(this.b_abrir_Click);
            // 
            // pb_etiquetas
            // 
            this.pb_etiquetas.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pb_etiquetas.Location = new System.Drawing.Point(234, 139);
            this.pb_etiquetas.Name = "pb_etiquetas";
            this.pb_etiquetas.Size = new System.Drawing.Size(160, 120);
            this.pb_etiquetas.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pb_etiquetas.TabIndex = 36;
            this.pb_etiquetas.TabStop = false;
            // 
            // pbUmbral
            // 
            this.pbUmbral.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pbUmbral.Location = new System.Drawing.Point(233, 13);
            this.pbUmbral.Name = "pbUmbral";
            this.pbUmbral.Size = new System.Drawing.Size(160, 120);
            this.pbUmbral.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pbUmbral.TabIndex = 35;
            this.pbUmbral.TabStop = false;
            this.pbUmbral.Click += new System.EventHandler(this.pbUmbral_Click);
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.cmdStart);
            this.groupBox3.Controls.Add(this.cmdStop);
            this.groupBox3.Controls.Add(this.cmdContinue);
            this.groupBox3.Controls.Add(this.label1);
            this.groupBox3.Controls.Add(this.numCaptureTime);
            this.groupBox3.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.groupBox3.ForeColor = System.Drawing.SystemColors.Desktop;
            this.groupBox3.Location = new System.Drawing.Point(475, 7);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(366, 74);
            this.groupBox3.TabIndex = 37;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Webcam";
            // 
            // groupBox4
            // 
            this.groupBox4.Controls.Add(this.b_calcular);
            this.groupBox4.Controls.Add(this.textBox1);
            this.groupBox4.ForeColor = System.Drawing.SystemColors.Desktop;
            this.groupBox4.Location = new System.Drawing.Point(22, 359);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new System.Drawing.Size(471, 158);
            this.groupBox4.TabIndex = 38;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "Momentos de Hu";
            // 
            // groupBox5
            // 
            this.groupBox5.Controls.Add(this.tb_abrir);
            this.groupBox5.Controls.Add(this.b_abrir);
            this.groupBox5.ForeColor = System.Drawing.SystemColors.Desktop;
            this.groupBox5.Location = new System.Drawing.Point(22, 12);
            this.groupBox5.Name = "groupBox5";
            this.groupBox5.Size = new System.Drawing.Size(438, 51);
            this.groupBox5.TabIndex = 39;
            this.groupBox5.TabStop = false;
            this.groupBox5.Text = "Imagen Archivada";
            // 
            // groupBox6
            // 
            this.groupBox6.Controls.Add(this.groupBox1);
            this.groupBox6.Controls.Add(this.groupBox7);
            this.groupBox6.Controls.Add(this.pb_etiquetas);
            this.groupBox6.Controls.Add(this.pbUmbral);
            this.groupBox6.Controls.Add(this.pictureBox1);
            this.groupBox6.Location = new System.Drawing.Point(22, 81);
            this.groupBox6.Name = "groupBox6";
            this.groupBox6.Size = new System.Drawing.Size(819, 272);
            this.groupBox6.TabIndex = 40;
            this.groupBox6.TabStop = false;
            this.groupBox6.Text = "Imagenes";
            // 
            // groupBox7
            // 
            this.groupBox7.Controls.Add(this.button3);
            this.groupBox7.Controls.Add(this.numericUpDown1);
            this.groupBox7.Location = new System.Drawing.Point(406, 18);
            this.groupBox7.Name = "groupBox7";
            this.groupBox7.Size = new System.Drawing.Size(413, 115);
            this.groupBox7.TabIndex = 37;
            this.groupBox7.TabStop = false;
            this.groupBox7.Text = "Filtro";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(20, 59);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(82, 13);
            this.label2.TabIndex = 13;
            this.label2.Text = "No. Objetos";
            // 
            // tb_objetos
            // 
            this.tb_objetos.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.tb_objetos.Enabled = false;
            this.tb_objetos.Location = new System.Drawing.Point(23, 80);
            this.tb_objetos.Name = "tb_objetos";
            this.tb_objetos.Size = new System.Drawing.Size(42, 20);
            this.tb_objetos.TabIndex = 14;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.Location = new System.Drawing.Point(24, 75);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(42, 13);
            this.label5.TabIndex = 15;
            this.label5.Text = "Area:";
            // 
            // n_area
            // 
            this.n_area.Location = new System.Drawing.Point(158, 0);
            this.n_area.Maximum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.n_area.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.n_area.Name = "n_area";
            this.n_area.Size = new System.Drawing.Size(57, 20);
            this.n_area.TabIndex = 16;
            this.n_area.Value = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.n_area.ValueChanged += new System.EventHandler(this.n_area_ValueChanged);
            // 
            // tb_area
            // 
            this.tb_area.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.tb_area.Enabled = false;
            this.tb_area.Location = new System.Drawing.Point(72, 73);
            this.tb_area.Name = "tb_area";
            this.tb_area.Size = new System.Drawing.Size(49, 20);
            this.tb_area.TabIndex = 17;
            // 
            // l_centroide
            // 
            this.l_centroide.AutoSize = true;
            this.l_centroide.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.l_centroide.Location = new System.Drawing.Point(17, 38);
            this.l_centroide.Name = "l_centroide";
            this.l_centroide.Size = new System.Drawing.Size(74, 13);
            this.l_centroide.TabIndex = 24;
            this.l_centroide.Text = "Centroide:";
            // 
            // l_x
            // 
            this.l_x.AutoSize = true;
            this.l_x.Location = new System.Drawing.Point(116, 41);
            this.l_x.Name = "l_x";
            this.l_x.Size = new System.Drawing.Size(15, 13);
            this.l_x.TabIndex = 22;
            this.l_x.Text = "x:";
            // 
            // l_y
            // 
            this.l_y.AutoSize = true;
            this.l_y.Location = new System.Drawing.Point(183, 41);
            this.l_y.Name = "l_y";
            this.l_y.Size = new System.Drawing.Size(15, 13);
            this.l_y.TabIndex = 23;
            this.l_y.Text = "y:";
            // 
            // button4
            // 
            this.button4.Enabled = false;
            this.button4.Location = new System.Drawing.Point(9, 23);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(105, 23);
            this.button4.TabIndex = 9;
            this.button4.Text = "Identificar";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // tb_y_img
            // 
            this.tb_y_img.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.tb_y_img.Enabled = false;
            this.tb_y_img.Location = new System.Drawing.Point(208, 36);
            this.tb_y_img.Name = "tb_y_img";
            this.tb_y_img.Size = new System.Drawing.Size(36, 20);
            this.tb_y_img.TabIndex = 23;
            // 
            // tb_x_img
            // 
            this.tb_x_img.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.tb_x_img.Enabled = false;
            this.tb_x_img.Location = new System.Drawing.Point(141, 36);
            this.tb_x_img.Name = "tb_x_img";
            this.tb_x_img.Size = new System.Drawing.Size(39, 20);
            this.tb_x_img.TabIndex = 22;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.groupBox8);
            this.groupBox1.Controls.Add(this.button4);
            this.groupBox1.Controls.Add(this.tb_objetos);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Location = new System.Drawing.Point(406, 139);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(415, 119);
            this.groupBox1.TabIndex = 38;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Etiquetamiento";
            // 
            // groupBox8
            // 
            this.groupBox8.Controls.Add(this.tb_x_img);
            this.groupBox8.Controls.Add(this.tb_y_img);
            this.groupBox8.Controls.Add(this.tb_area);
            this.groupBox8.Controls.Add(this.l_centroide);
            this.groupBox8.Controls.Add(this.label5);
            this.groupBox8.Controls.Add(this.n_area);
            this.groupBox8.Controls.Add(this.l_x);
            this.groupBox8.Controls.Add(this.l_y);
            this.groupBox8.ForeColor = System.Drawing.SystemColors.Desktop;
            this.groupBox8.Location = new System.Drawing.Point(120, 0);
            this.groupBox8.Name = "groupBox8";
            this.groupBox8.Size = new System.Drawing.Size(293, 119);
            this.groupBox8.TabIndex = 25;
            this.groupBox8.TabStop = false;
            this.groupBox8.Text = "           Objeto seleccionado";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(853, 539);
            this.Controls.Add(this.groupBox6);
            this.Controls.Add(this.groupBox5);
            this.Controls.Add(this.groupBox4);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Name = "Form1";
            this.Text = "Vision Computadora";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.numCaptureTime)).EndInit();
            this.groupBox2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pb_etiquetas)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbUmbral)).EndInit();
            this.groupBox3.ResumeLayout(false);
            this.groupBox4.ResumeLayout(false);
            this.groupBox4.PerformLayout();
            this.groupBox5.ResumeLayout(false);
            this.groupBox5.PerformLayout();
            this.groupBox6.ResumeLayout(false);
            this.groupBox6.PerformLayout();
            this.groupBox7.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.n_area)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox8.ResumeLayout(false);
            this.groupBox8.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			// set the image capture size
			this.WebCamCapture.CaptureHeight = this.pictureBox1.Height;
			this.WebCamCapture.CaptureWidth = this.pictureBox1.Width;
		}


         ///////////////////////////////////////////////robotica
        public void ConvertToGrayscale(Bitmap source)
        {
            Bitmap bm = new Bitmap(source.Width, source.Height);
            int j = 0;
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = source.GetPixel(x, y);
                    int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                    gris[luma] = (gris[luma]) + 1;
                    bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
                    j++;
                }
            }
            set_bmp_gris(bm);
        }

        public void set_bmp_gris(Bitmap bmp_gris)
        {
            this.bmp_gris = bmp_gris;
        }

        public Bitmap Umbral(Bitmap source, int u)
        {
            Bitmap bm = new Bitmap(source.Width, source.Height);
            int j = 0;
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = source.GetPixel(x, y);
                    int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                    if (luma <= u)
                    {
                        luma = 0;
                    }
                    else if (luma > u)
                    {
                        luma = 255;
                    }
                    bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
                    j++;
                }
            }
            return bm;
        }



        public Bitmap generaUmbral()
        {
            int valor = (int)numericUpDown1.Value;
            Bitmap bin = Umbral(bmp, valor);
            bin.Save("obj.bmp");
            return bin;
        }


        public void matBin()
        {
            Bitmap bit = generaUmbral();
            int[,] mat = new int[bit.Width, bit.Height];
            for (int i = 0; i < bit.Width; i++)
            {
                for (int j = 0; j < bit.Height; j++)
                {
                    Color c = bit.GetPixel(i, j);
                    if (c.R == 0 && c.G == 0 && c.B == 0)
                    {
                        mat[i, j] = 1;
                    }
                }
            }

            int[,] matR = new int[bit.Height, bit.Width];
            for (int i = 0; i < bit.Width; i++)
            {
                for (int j = 0; j < bit.Height; j++)
                {
                    matR[j, i] = mat[i, j];
                }
            }
            pMat(matR);
            matB = matR;

        }

        public void pMat(int[,] mat)
        {

            TextWriter tw = new StreamWriter("imagen.txt");
            for (int i = 0; i < mat.GetLength(0); i++)
            {
                for (int j = 0; j < mat.GetLength(1); j++)
                {
                    tw.Write(mat[i, j].ToString());
                }
                tw.WriteLine(" ");
            }
            tw.Close();
        }


        public void initSeg()
        {
            int numero = 2;
            for (int i = 0; i < matB.GetLength(0); i++)
            {
                for (int j = 0; j < matB.GetLength(1); j++)
                {
                    if (matB[i, j] == 1)
                    {
                        matB[i, j] = numero;
                        segRec(i, j, numero);
                        numero++;
                    }
                }
            }
            pMat(matB);
        }


        public void segRec(int i, int j, int num)
        {


            try
            {
                if (matB[i, j] == 0) { return; }

            }
            catch (IndexOutOfRangeException)
            {


            }
            //if(i>0 && j>0)

            try
            {
                if (matB[i - 1, j - 1] == 1)
                {

                    matB[i - 1, j - 1] = num;
                    segRec(i - 1, j - 1, num);

                }
            }
            catch (Exception) { }


            try
            {
                if (matB[i - 1, j] == 1)
                {

                    matB[i - 1, j] = num;
                    segRec(i - 1, j, num);

                }
            }
            catch (Exception) { }


            try
            {
                if (matB[i - 1, j + 1] == 1)
                {

                    matB[i - 1, j + 1] = num;
                    segRec(i - 1, j + 1, num);

                }
            }
            catch (Exception) { }

            try
            {
                if (matB[i, j - 1] == 1)
                {

                    matB[i, j - 1] = num;
                    segRec(i, j - 1, num);

                }
            }
            catch (Exception) { }


            try
            {
                if (matB[i, j + 1] == 1)
                {

                    matB[i, j + 1] = num;
                    segRec(i, j + 1, num);

                }
            }
            catch (Exception) { }

            try
            {
                if (matB[i + 1, j - 1] == 1)
                {

                    matB[i + 1, j - 1] = num;
                    segRec(i + 1, j - 1, num);

                }
            }
            catch (Exception) { }


            try
            {
                if (matB[i + 1, j] == 1)
                {

                    matB[i + 1, j] = num;
                    segRec(i + 1, j, num);

                }
            }
            catch (Exception) { }


            try
            {
                if (matB[i + 1, j + 1] == 1)
                {

                    matB[i + 1, j + 1] = num;
                    segRec(i + 1, j + 1, num);

                }

            }
            catch (Exception) { }

        }

        public void segmentacion()
        {


            /*
             xyyyyyyyyyyyyyyyyyy 
             x 
             x
             x
             x
             * */

            getNumeros(matB);
            //print_arr(objetos);
            for (int k = 1; k <= cantidad_objetos(); k++)
            {

                area(k);
                if (ar < area_minima)
                {

                    for (int i = 0; i < matB.GetLength(0); i++)
                    {
                        for (int j = 0; j < matB.GetLength(1); j++)
                        {
                            if (matB[i, j] == k)
                            {
                                //textBox1.Text = "entro";
                                matB[i, j] = 0;
                            }
                        }
                    }
                }
            }
            limpia_arr(objetos);
            getNumeros(matB);


            ///////////////CAMBIA NUMEROS


            for (int i = 0; i < matB.GetLength(0); i++)
            {
                for (int j = 0; j < matB.GetLength(1); j++)
                {
                    if (matB[i, j] != 0)
                    {
                        matB[i, j] = busca_indice(objetos, matB[i, j]);
                    }
                }
            }

            pMat(matB);
            set_mat(matB);

        }



        public void set_mat(int[,] mat)
        {
            matriz = mat;
        }

        public void set_matrizBin(int[,] mat)
        {
            matrizBin = mat;
        }

        public int cantidad_objetos()
        {
            int num = 0;
            for (int i = 1; i < objetos.Length; i++)
            {
                if (objetos[i] != 0)
                {
                    num = i;
                }
            }

            return num;
        }
        public void print_arr(int[] arr)
        {
            for (int k = 0; k < arr.Length; k++)
            {
                textBox1.AppendText("\r\n" + arr[k]);
            }
        }

        public int busca_indice(int[] arr, int num)
        {
            int indice = 0;

            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == num)
                {
                    indice = i;
                }
            }
            return indice;
        }

        //BUSCA LOS OBJETOS
        public void getNumeros(int[,] mat)
        {
            int obj = 1;
            for (int i = 0; i < mat.GetLength(0); i++)
            {
                for (int j = 0; j < mat.GetLength(1); j++)
                {
                    if (mat[i, j] != 0)
                    {
                        if (!buscaNumero(objetos, mat[i, j]))
                        {
                            objetos[obj] = mat[i, j];
                            obj++;
                        }
                    }
                }
            }

        }

        public bool buscaNumero(int[] arr, int num)
        {
            for (int p = 0; p < arr.Length; p++)
            {
                if (arr[p] == num)
                {
                    return true;
                }

            }
            return false;
        }

        public void cambiaMat(int[,] mat)
        {
            for (int i = 0; i < mat.GetLength(0); i++)
            {
                for (int j = 0; j < mat.GetLength(1); j++)
                {
                    if (mat[i, j] == 1)
                    {
                        mat[i, j] = 2;
                    }
                }
            }
            pMat(mat);
        }

        public void limpia_arr(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = 0;
            }
        }
        public void limpia_arr(double[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = 0;
            }
        }
        public void limpia_arr(double[,] arr)
        {
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    arr[i,j] = 0; 
                }
            }
        }

        public void a_Excel()
        {
            Workbook book = new Workbook();
            Worksheet sheet = book.Worksheets.Add("Momentos");

            for (int i = 0; i < tabla.GetLength(0); i++)
            {
                WorksheetRow row = sheet.Table.Rows.Add();
                for (int j = 0; j < tabla.GetLength(1); j++)
                {

                    row.Cells.Add(tabla[i, j]);

                }
            }
            book.Save(@"c:\test.xls");
        }


        public double M(int p, int q, int num_obj)
        {
            double momento = 0;
            for (int i = 0; i < matB.GetLength(0); i++)
            {
                for (int j = 0; j < matB.GetLength(1); j++)
                {
                    if (matB[i, j] == num_obj)
                    {
                        num = 1;
                    }
                    else { num = 0; }

                    momento += Math.Pow(i + 1, p) * Math.Pow(j + 1, q) * num;
                }
            }
            return momento;
        }

        public void elongacion()
        {
            el = 0;
            el = (N20 + N02 + Math.Sqrt(Math.Pow(N20 - N02, 2) + 4 * Math.Pow(N11, 2))) / (N20 + N02 - Math.Sqrt(Math.Pow(N20 - N02, 2) + 4 * Math.Pow(N11, 2)));
            tabla[objeto, 8] = el.ToString();
        }

        public void angulo()
        {
            teta = 0;
            teta = .5 * Math.Atan2(2 * N11, N20 + N02) * 180 / Math.PI;
            tabla[objeto, 9] = teta.ToString();
            objeto++;
        }

        public void formulas()
        {
            int numero= objeto;
            string f="=MIN(B2:B"+numero+")";
            tabla[objeto, 1] = f;

             f = "=MIN(B2:B" + numero + ")";
            tabla[objeto, 2] = f;

             f = "=MIN(C2:C" + numero + ")";
            tabla[objeto, 3] = f;

             f = "=MIN(D2:D" + numero + ")";
            tabla[objeto, 4] = f;

             f = "=MIN(E2:E" + numero + ")";
            tabla[objeto, 5] = f;

             f = "=MIN(F2:F" + numero + ")";
            tabla[objeto, 6] = f;

             f = "=MIN(G2:G" + numero + ")";
            tabla[objeto, 7] = f;

             f = "=MIN(H2:H" + numero + ")";
            tabla[objeto, 8] = f;

             f = "=MIN(I2:I" + numero + ")";
            tabla[objeto, 9] = f;

            a_Excel();

            ////max
            objeto++;
            f = "=MAX(B2:B" + numero + ")";
            tabla[objeto, 1] = f;

            f = "=MAX(B2:B" + numero + ")";
            tabla[objeto, 2] = f;

            f = "=MAX(C2:C" + numero + ")";
            tabla[objeto, 3] = f;

            f = "=MAX(D2:D" + numero + ")";
            tabla[objeto, 4] = f;

            f = "=MAX(E2:E" + numero + ")";
            tabla[objeto, 5] = f;

            f = "=MAX(F2:F" + numero + ")";
            tabla[objeto, 6] = f;

            f = "=MAX(G2:G" + numero + ")";
            tabla[objeto, 7] = f;

            f = "=MAX(H2:H" + numero + ")";
            tabla[objeto, 8] = f;

            f = "=MAX(I2:I" + numero + ")";
            tabla[objeto, 9] = f;

            a_Excel();
        }


        public double Miu(int p, int q, int num_obj)
        {
            double momento = 0;
            for (int i = 0; i < matB.GetLength(0); i++)
            {
                for (int j = 0; j < matB.GetLength(1); j++)
                {
                    if (matB[i, j] == num_obj)
                    {
                        num = 1;
                    }
                    else { num = 0; }
                    momento += Math.Pow(i - x + 1, p) * Math.Pow(j - y + 1, q) * num;
                }
            }
            return momento;
        }
        public void etiquetado()
        {
            Bitmap bm = new Bitmap(bmp.Width, bmp.Height);

            for (int x = 0; x < bm.Height; x++)
            {
                for (int y = 0; y < bm.Width; y++)
                {
                    if (matB[x, y] != 0)
                    {
                        bm.SetPixel(y, x, Color.FromArgb(255, 255, 255));
                    }
                    if (matB[x, y] == (int)n_area.Value)
                    {
                        bm.SetPixel(y, x, Color.FromArgb(0, 0, 0));
                    }

                }
            }

            set_etiqueta(bm);
        }


        public void set_etiqueta(Bitmap bmp)
        {
            bmp_etiqueta = bmp;
        }

        public void centroide(int valor)
        {
            y_centro_img = (int)(M(0, 1, valor) / ar);
            x_centro_img = (int)(M(1, 0, valor) / ar);
            y = (M(0, 1, valor) / ar);
            x = (M(1, 0, valor) / ar);

            try
            {
                bmp_etiqueta.SetPixel(y_centro_img, x_centro_img, Color.Red);
                bmp_etiqueta.SetPixel(y_centro_img + 1, x_centro_img, Color.Red);
                bmp_etiqueta.SetPixel(y_centro_img - 1, x_centro_img, Color.Red);
                bmp_etiqueta.SetPixel(y_centro_img, x_centro_img + 1, Color.Red);
                bmp_etiqueta.SetPixel(y_centro_img, x_centro_img - 1, Color.Red);
                bmp_etiqueta.SetPixel(y_centro_img + 1, x_centro_img + 1, Color.Red);
                bmp_etiqueta.SetPixel(y_centro_img + 1, x_centro_img - 1, Color.Red);
                bmp_etiqueta.SetPixel(y_centro_img - 1, x_centro_img + 1, Color.Red);
                bmp_etiqueta.SetPixel(y_centro_img - 1, x_centro_img - 1, Color.Red);
              
            }
            catch (ArgumentOutOfRangeException) { }
            tb_x_img.Text = x_centro_img.ToString();
            tb_y_img.Text = y_centro_img.ToString();
        }

        public void ejes(int num)
        {
            double a = 0;
            double b = 0;
            double c = 0;
            double xij = 0;
            double yij = 0;
            double angulo1 = 0;
            double angulo2 = 0;
            double angulo3 = 0;
            double angulo4 = 0;


            /////calcular a

            for (int i = 0; i < matriz.GetLength(0); i++)
            {
                for (int j = 0; j < matriz.GetLength(1); j++)
                {
                    if (matriz[i, j] == num)
                    {
                        num = 1;
                    }
                    else { num = 0; }
                    xij = i - x_centro_img;
                    yij = j - y_centro_img;

                    a += Math.Pow(xij, 2) * (num);
                    b += xij * yij * (num);
                    c += Math.Pow(yij, 2) * (num);
                }
            }
            textBox1.AppendText("\r\na= " + a.ToString() + "\r\nb= " + b.ToString() + "\r\nc= " + c.ToString());
            //frac1 = (b / -(Math.Sqrt(Math.Pow(b,2)+Math.Pow(a-c,2)))) / 2;
            //frac2 = (b / +(Math.Sqrt(Math.Pow(b, 2) + Math.Pow(a - c, 2)))) / 2;
            angulo1 = Math.Asin((b / -(Math.Sqrt(Math.Pow(b, 2) + Math.Pow(a - c, 2)))) / 2);
            angulo2 = Math.Asin((b / +(Math.Sqrt(Math.Pow(b, 2) + Math.Pow(a - c, 2)))) / 2);

            angulo3 = Math.Acos(((a - c) / -(Math.Sqrt(Math.Pow(b, 2) + Math.Pow(a - c, 2)))) / 2);
            angulo4 = Math.Acos(((a - c) / +(Math.Sqrt(Math.Pow(b, 2) + Math.Pow(a - c, 2)))) / 2);

            //textBox1.AppendText("\r\nangulo1= " + angulo1.ToString() + "\r\nangulo2= " + angulo2.ToString());
            //textBox1.AppendText("\r\nangulo3= " + angulo1.ToString() + "\r\nangulo4= " + angulo2.ToString());

        }


        public void calcula_Ns(int valor)
        {
            int chi = 0;
            chi = (1 + 1 + 2) / 2;
            N11 = (1 / Math.Pow(ar, chi)) * Miu(1, 1, valor);

            chi = (1 + 2 + 2) / 2;
            N12 = (1 / Math.Pow(ar, chi)) * Miu(1, 2, valor);

            chi = (2 + 1 + 2) / 2;
            N21 = (1 / Math.Pow(ar, chi)) * Miu(2, 1, valor);

            chi = (0 + 2 + 2) / 2;
            N02 = (1 / Math.Pow(ar, chi)) * Miu(0, 2, valor);

            chi = (0 + 3 + 2) / 2;
            N03 = (1 / Math.Pow(ar, chi)) * Miu(0, 3, valor);

            chi = (1 + 0 + 2) / 2;
            N10 = (1 / Math.Pow(ar, chi)) * Miu(1, 0, valor);

            chi = (2 + 0 + 2) / 2;
            N20 = (1 / Math.Pow(ar, chi)) * Miu(2, 0, valor);

            chi = (3 + 0 + 2) / 2;
            N30 = (1 / Math.Pow(ar, chi)) * Miu(3, 0, valor);

            //
            //textBox1.AppendText("\r\nLISTO");
        }

        public void area(int valor)
        {
            ar = 0;
            ar = M(0, 0, valor);
        }

        public void momentos_Hu()
        {
            M1 = 0;
            M2 = 0;
            M3 = 0;
            M4 = 0;
            M5 = 0;
            M6 = 0;
            M7 = 0;
            M1 = N20 + N02;
            M2 = Math.Pow((N20 - N02), 2) + 4 * Math.Pow(N11, 2);
            M3 = Math.Pow((N30 - 3 * N12), 2) + Math.Pow(((3 * N21) - N03), 2);
            M4 = Math.Pow(N30 + N12, 2) + Math.Pow(N21 - N03, 2);
            M5 = (N30 - 3 * N12) * (N30 + N12) * (Math.Pow(N30 + N12, 2) - 3 * Math.Pow(N21 - N03, 2)) + ((3 * N21 - N03) * (N21 + N03) * (3 * Math.Pow(N30 + N12, 2) - Math.Pow(N21 + N03, 2)));
            M6 = (N20 - N02) * (Math.Pow(N30 + N12, 2) - Math.Pow(N21 + N03, 2)) + (4 * N11) * (N30 + N12) * (N21 + N03);
            M7 = (3 * N21 - N03) * (N30 + N12) * (Math.Pow(N30 + N12, 2) - 3 * Math.Pow(N21 + N03, 2)) + (N30 - 3 * N12) * (N21 + N03) * (3 * Math.Pow(N30 + N12, 2) - Math.Pow(N21 + N03, 2));





            tabla[objeto, 0] = tb_abrir.Text;
            tabla[objeto, 1] = M1.ToString();
            tabla[objeto, 2] = M2.ToString();
            tabla[objeto, 3] = M3.ToString();
            tabla[objeto, 4] = M4.ToString();
            tabla[objeto, 5] = M5.ToString();
            tabla[objeto, 6] = M6.ToString();
            tabla[objeto, 7] = M7.ToString();


            

            //textBox1.AppendText("\r\nM1= " + M1.ToString() + "\r\nM2= " + M2.ToString() + "\r\nM3= " + M3.ToString() + "\r\nM4= " + M4.ToString() + "\r\nM5= " + M5.ToString() + "\r\nM6= " + M6.ToString() + "\r\nM7= " + M7.ToString() + "\r\nElongacion= " + elongacion.ToString());

            //MessageBox.Show("LISTO");

            //
            //textBox1.AppendText("LISTO");
        }

        public void listaArchivos()
        {
            DirectoryInfo dir = new DirectoryInfo(@"C:\Fotos\net\moneda");
            FileInfo[] bmpfiles = dir.GetFiles("*.jpg");
            textBox1.AppendText("Total number of bmp files: " + bmpfiles.Length.ToString());

            foreach (FileInfo f in bmpfiles)
            {
                textBox1.AppendText("\r\n" + f.Name);
                textBox1.AppendText("\r\n" + f.Length.ToString());
                textBox1.AppendText("\r\n" + f.CreationTime.ToString());
                textBox1.AppendText("\r\n" + f.Attributes.ToString());
            }

        }
        public void abre()
        {

            FileInfo[] bmpfiles = dir.GetFiles("*.jpg");
            textBox1.AppendText(index.ToString() + "\r\n");



            tb_abrir.Text = dir + bmpfiles[index].Name;
            Bitmap bmpR = new Bitmap(tb_abrir.Text.ToString());
            bmp = ResizeBitmap(bmpR, 160, 120);
            bmpR.Dispose();
            pictureBox1.Image = bmp;

        }

        public void calculo_automatico()
        {
            ///asegurarse que las fotos siguen la sintaxis y no se saltean: Picture 0+numero
            ///cambiar la carpeta cuchillo por la correspondiente al objeto
            for (int i = 1; i <= 50; i++)/////el valor 50 es el numero de fotos
            {
                string dir = @"C:\Fotos\net\moneda\Picture 0" + i.ToString() + ".jpg";


                tb_abrir.Text = dir;
                bmp = new Bitmap(tb_abrir.Text.ToString());


                pbUmbral.Image = generaUmbral();//umbral

                textBox1.Text = "";
                limpia_arr(objetos);//inicializa en 0 el arreglo
                matBin();//Binariza la imagen
                initSeg();//Segmenta la imagen
                segmentacion();//numera cada objeto


                n_area.Value = 1;
                area((int)n_area.Value);
                centroide((int)n_area.Value);

                calcula_Ns((int)n_area.Value);
                momentos_Hu();
                elongacion();
                angulo();

                a_Excel();
                textBox1.AppendText("mision completa" + i + "\r\n");


            }
        }

        public void identifica()
        {
            //limpia_arr(val);
            limpia_arr(dif);
            int indice = 0 ;

            for (int i = 0; i < val.GetLength(0); i++)
            {
                dif[i] = Math.Abs((val[i, 0] - M1))+Math.Abs((val[i, 1] - M2));
            }
            //textBox1.AppendText("val0= " + val[0,0].ToString() + "\r\n");
            //textBox1.AppendText("val1= " + val[1,0].ToString() + "\r\n");
            //textBox1.AppendText("val2= " + val[2,0].ToString() + "\r\n");

            //textBox1.AppendText("dif0= " + dif[0].ToString() + "\r\n");
            //textBox1.AppendText("dif1= " + dif[1].ToString() + "\r\n");
            //textBox1.AppendText("dif1= " + dif[2].ToString() + "\r\n");
            //textBox1.AppendText("0= " + val.GetLength(0).ToString() + "\r\n");
            
            double min = dif[0];

            for (int i = 1; i < dif.Length; i++)
            {
                if(dif[i] != 0)
                {
                if(min>dif[i])
                    {
                        min = dif[i];
                        indice = i;
                    }

                }

            }

            //textBox1.AppendText("indice= " + indice.ToString() + "\r\n");

            switch (indice)
            {
                case 0:
                    textBox1.AppendText("billete\r\n");
                    break;
                case 1:
                    textBox1.AppendText("Cartera\r\n");
                    break;
                case 2:
                    textBox1.AppendText("Clavo\r\n");
                    break;
                case 3:
                    textBox1.AppendText("Cuchara\r\n");
                    break;
                case 4:
                    textBox1.AppendText("Cuchillo\r\n");
                    break;
                case 5:
                    textBox1.AppendText("Desarmador\r\n");
                    break;
                case 6:
                    textBox1.AppendText("Cepillo\r\n");
                    break;
                case 7:
                    textBox1.AppendText("Lapiz\r\n");
                    break;
                case 8:
                    textBox1.AppendText("Libro\r\n");
                    break;
                case 9:
                    textBox1.AppendText("Llave\r\n");
                    break;
                case 10:
                    textBox1.AppendText("Martillo\r\n");
                    break;
                case 11:
                    textBox1.AppendText("Moneda\r\n");
                    break;
                case 12:
                    textBox1.AppendText("Peine\r\n");
                    break;
                case 13:
                    textBox1.AppendText("Pinzas\r\n");
                    break;
                case 14:
                    textBox1.AppendText("Pinzas\r\n");
                    break;
                case 15:
                    textBox1.AppendText("Plato\r\n");
                    break;
                case 16:
                    textBox1.AppendText("Pluma\r\n");
                    break;
                case 17:
                    textBox1.AppendText("Stealson\r\n");
                    break;
                case 18:
                    textBox1.AppendText("Tenedor\r\n");
                    break;
                case 19:
                    textBox1.AppendText("Tijeras\r\n");
                    break;
                case 20:
                    textBox1.AppendText("Tijeras\r\n");
                    break;
                case 21:
                    textBox1.AppendText("Tornillo\r\n");
                    break;
                default:
                    textBox1.AppendText("OTRO\r\n");
                    break;
            }
/*
  if(indice == 0){
      textBox1.AppendText("billete\r\n");
      //textBox1.Text = "Billete";
  }
            if(indice ==1){
                textBox1.AppendText("Cartera\r\n");
                //textBox1.Text = "Cartera";
            }
            
 */

        }



        /////////////////////////////////////////////////////

        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// stop the video capture
			this.WebCamCapture.Stop();
		}

		/// <summary>
		/// An image was capture
		/// </summary>
		/// <param name="source">control raising the event</param>
		/// <param name="e">WebCamEventArgs</param>
		private void WebCamCapture_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e)
		{
			// set the picturebox picture
			this.pictureBox1.Image = e.WebCamImage;
		}

		private void cmdStart_Click(object sender, System.EventArgs e)
		{
			// change the capture time frame
			this.WebCamCapture.TimeToCapture_milliseconds = (int) this.numCaptureTime.Value;

			// start the video capture. let the control handle the
			// frame numbers.
			this.WebCamCapture.Start(0);

		}

		private void cmdStop_Click(object sender, System.EventArgs e)
		{
			// stop the video capture
			this.WebCamCapture.Stop();
            limpia_arr(val);
            Bitmap bmpR = (Bitmap) pictureBox1.Image;
            bmp = ResizeBitmap(bmpR, 160, 120);
            bmpR.Dispose();
		}

		private void cmdContinue_Click(object sender, System.EventArgs e)
		{
			// change the capture time frame
			this.WebCamCapture.TimeToCapture_milliseconds = (int) this.numCaptureTime.Value;

			// resume the video capture from the stop
			this.WebCamCapture.Start(this.WebCamCapture.FrameNumber);
		}

       

        private void button3_Click(object sender, EventArgs e)
        {
            pbUmbral.Image = generaUmbral();

            pbUmbral.SizeMode = PictureBoxSizeMode.AutoSize;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            limpia_arr(objetos);//inicializa en 0 el arreglo
            matBin();//Binariza la imagen
            initSeg();//Segmenta la imagen
            segmentacion();//numera cada objeto


            tb_objetos.Text = cantidad_objetos().ToString();//cuenta los objetos existentes

            n_area.Maximum = cantidad_objetos();
            n_area.Value = 1;
            //area_minima = Convert.ToInt16(tb_areamin.Text);
            area((int)n_area.Value);

            tb_area.Text = ar.ToString();

            etiquetado();
            pb_etiquetas.Image = bmp_etiqueta;
            pb_etiquetas.SizeMode = PictureBoxSizeMode.AutoSize;

            b_calcular.Enabled = true;

        }

        private void b_calcular_Click(object sender, EventArgs e)
        {
            area((int)n_area.Value);
            textBox1.AppendText("Area = " + ar + "\r\n");
            tb_area.Text = ar.ToString();
            centroide((int)n_area.Value);
            textBox1.AppendText("X = " + x.ToString() + "\r\n");
            textBox1.AppendText("Y = " + y.ToString() + "\r\n");

            calcula_Ns((int)n_area.Value);

            textBox1.AppendText("N02= " + N02.ToString() + "\r\n");
            textBox1.AppendText("N03= " + N03.ToString() + "\r\n");
            textBox1.AppendText("N11= " + N11.ToString() + "\r\n");
            textBox1.AppendText("N12= " + N12.ToString() + "\r\n");
            textBox1.AppendText("N20= " + N20.ToString() + "\r\n");
            textBox1.AppendText("N21= " + N21.ToString() + "\r\n");
            textBox1.AppendText("N30= " + N30.ToString() + "\r\n");

            momentos_Hu();
            textBox1.AppendText("M1= " + M1.ToString() + "\r\n");
            textBox1.AppendText("M2= " + M2.ToString() + "\r\n");
            textBox1.AppendText("M3= " + M3.ToString() + "\r\n");
            textBox1.AppendText("M4= " + M4.ToString() + "\r\n");
            textBox1.AppendText("M5= " + M5.ToString() + "\r\n");
            textBox1.AppendText("M6= " + M6.ToString() + "\r\n");
            textBox1.AppendText("M7= " + M7.ToString() + "\r\n");

            elongacion();
            textBox1.AppendText("elongacion= " + el.ToString() + "\r\n");

            angulo();

            textBox1.AppendText("teta= " + teta.ToString() + "\r\n");

            pb_etiquetas.Image = bmp_etiqueta;
            a_Excel();





         
        }

        private void b_excel_Click(object sender, EventArgs e)
        {
            //formulas();
            System.Diagnostics.Process.Start("c:\\test.xls");
        }

        private void n_area_ValueChanged(object sender, EventArgs e)
        {
            textBox1.Text = "";
            etiquetado();
            area((int)n_area.Value);

            tb_area.Text = ar.ToString();
            pb_etiquetas.Image = bmp_etiqueta;
        }

        private void b_automatico_Click(object sender, EventArgs e)
        {
            calculo_automatico();
            System.Diagnostics.Process.Start("c:\\test.xls");
        }

        private void b_identifica_Click(object sender, EventArgs e)
        {
            identifica();
        }

        public Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
        {
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
            return result;
        }

        private void b_abrir_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpenDialog = new OpenFileDialog();
            OpenDialog.Title = "Open Image...";
            OpenDialog.InitialDirectory = @"C:\";
            //OpenDialog.Filter = "Image files (*.jpg, *.bmp)|*.jpg;*.bmp;|All files (*.*)|*.*";
            OpenDialog.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif|Bitmaps (*.bmp)|*.bmp";
            OpenDialog.FilterIndex = 1;
            OpenDialog.RestoreDirectory = true;
            if (OpenDialog.ShowDialog() == DialogResult.OK)
            {
                //Abro el dialogo y obtengo el path
                tb_abrir.Text = OpenDialog.FileName;
                Bitmap bmpR = new Bitmap(tb_abrir.Text.ToString());
                bmp = ResizeBitmap(bmpR, 160, 120);
                bmpR.Dispose();
                pictureBox1.Image = bmp;
                button3.Enabled = true;
                button4.Enabled = true;

            }

            
            N02 = 0;
            N03 = 0;
            N10 = 0;
            N20 = 0;
            N30 = 0;

            N11 = 0;
            N12 = 0;

            N21 = 0;

            matB = null;
            matriz = null;
            ar = 0;
            y_centro_img = 0;
            x_centro_img = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            abre();

            N02 = 0;
            N03 = 0;
            N10 = 0;
            N20 = 0;
            N30 = 0;

            N11 = 0;
            N12 = 0;

            N21 = 0;

            matB = null;
            matriz = null;
            ar = 0;
            y_centro_img = 0;
            x_centro_img = 0;
        }

        private void pbUmbral_Click(object sender, EventArgs e)
        {

        }
	}
}
